package com.peanut.huibutie.controller;

import com.alibaba.fastjson.JSON;
import com.peanut.huibutie.common.AppConfig;
import com.peanut.huibutie.common.MD5SignUtils;
import com.peanut.huibutie.entity.PayResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 接收会补贴的通知回调
 */
@RestController
@RequestMapping("/notify")
@Slf4j
public class HandleNotifyController {

    @Resource
    private AppConfig appConfig;

    /**
     * 接收会补贴的支付通知回调
     *
     * @param reqMap
     * @return
     */
    @PostMapping(value = "/pay_notify")
    @ResponseBody
    public String handlePayNotify(@RequestBody Map reqMap) {

        log.info("会补贴异步通知返回报文:" + reqMap);
        boolean verifySign = MD5SignUtils.verifySign(appConfig.getAppSecret(), reqMap);
        if (!verifySign) {
            // "验签失败"
            return "FAIL:验签失败";
        }

        PayResultVO payResultVO = JSON.parseObject(JSON.toJSONString(reqMap), PayResultVO.class);
        log.info("会补贴异步通知返回结果-notifyData:{}", payResultVO);
        // 业务处理

        //业务处理完成，回写
        return "SUCCESS";
    }
}
